package com.ea.controller.back;

import com.ea.api.controller.admin.BackElectricGoodsInfoApi;
import com.ea.common.enums.OperationResultCode;
import com.ea.common.exception.ServiceException;
import com.ea.common.response.JsonResult;
import com.ea.common.util.TypeConversionToJson;
import com.ea.domain.dto.ElectricGoodCreateDTO;
import com.ea.domain.dto.ElectricGoodUpdateDTO;
import com.ea.domain.dto.ElectricStatusUpdateDTO;
import com.ea.domain.dto.ElectricSkuCreateDTO;
import com.ea.domain.dto.ElectricSkuUpdateDTO;
import com.ea.domain.query.BackElectricGoodsQueryDTO;
import com.ea.domain.query.BackElectricSkuQueryDTO;
import com.ea.service.IElectricGoodsInfoService;
import com.ea.service.IEletricGoodsSkuInfoService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * <p>
 * 电器信息表 前端控制器
 * </p>
 *
 * @author 电器商城
 * @since 2022-11-29
 */
@RestController
@RequestMapping("/electric/back/store")
public class ElectricGoodsInfoController implements BackElectricGoodsInfoApi {


    @Autowired
    private IElectricGoodsInfoService goodsInfoService;

    @Autowired
    private IEletricGoodsSkuInfoService skuInfoService;


    @Override
    @PostMapping("/goods/list")
    public JsonResult getGoodsList(@RequestBody String queryStr) {
        BackElectricGoodsQueryDTO queryDTO = TypeConversionToJson.jsonToEntity(queryStr, BackElectricGoodsQueryDTO.class);
        return JsonResult.doSuccessful(goodsInfoService.getGoodsInfoList(queryDTO));
    }

    @Override
    @PostMapping("/goods/create")
    public JsonResult createElectricGood(@RequestBody String goodsStr) {
        if (StringUtils.isEmpty(goodsStr)) {
            throw new ServiceException(OperationResultCode.PARAMETER_ERROR);
        }
        ElectricGoodCreateDTO createDTO = TypeConversionToJson.jsonToEntity(goodsStr, ElectricGoodCreateDTO.class);
        goodsInfoService.createElectricGoods(createDTO);
        return JsonResult.doSuccessful();
    }

    @Override
    @PostMapping("/goods/update")
    public JsonResult updateElectricGood(@RequestBody ElectricGoodUpdateDTO updateDTO) {
        if (updateDTO == null) {
            throw new ServiceException(OperationResultCode.PARAMETER_ERROR);
        }
        goodsInfoService.changeElectricGoods(updateDTO);
        return JsonResult.doSuccessful();
    }

    @Override
    @PostMapping("/goods/update/status")
    public JsonResult updateElectricGoodStatus(@RequestBody ElectricStatusUpdateDTO updateDTO) {

        if (updateDTO == null || updateDTO.getBaseId() == null || updateDTO.getStatus() == null) {
            throw new ServiceException(OperationResultCode.PARAMETER_ERROR);
        }
        goodsInfoService.changeElectricGoodsStatus(updateDTO.getBaseId(), updateDTO.getStatus());
        return JsonResult.updateSuccessFul();
    }

    @Override
    @PostMapping("/goods/update/block")
    public JsonResult updateElectricBlockStatus(@RequestBody ElectricStatusUpdateDTO updateDTO) {
        if (updateDTO == null || updateDTO.getBaseId() == null || updateDTO.getStatus() == null) {
            throw new ServiceException(OperationResultCode.PARAMETER_ERROR);
        }
        goodsInfoService.changeElectricBlockStatus(updateDTO.getBaseId(), updateDTO.getStatus());
        return JsonResult.updateSuccessFul();
    }

    @Override
    @PostMapping("/goods/sku/create")
    public JsonResult createGoodsSku(@RequestBody String skuStr) {
        if (StringUtils.isEmpty(skuStr)) {
            throw new ServiceException(OperationResultCode.PARAMETER_ERROR);
        }
        List<ElectricSkuCreateDTO> createDTOList = TypeConversionToJson.jsonToList(skuStr, ElectricSkuCreateDTO.class);
        if (CollectionUtils.isEmpty(createDTOList)) {
            throw new ServiceException(OperationResultCode.CREATE_SKU_INFO_ERROR);
        }
        skuInfoService.createGoodsSku(createDTOList);
        return JsonResult.doSuccessful();
    }

    @Override
    @PostMapping("/goods/sku/update")
    public JsonResult updateGoodsSku(@RequestBody String skuStr) {
        if (StringUtils.isEmpty(skuStr)) {
            throw new ServiceException(OperationResultCode.PARAMETER_ERROR);
        }
        ElectricSkuUpdateDTO skuInfo = TypeConversionToJson.jsonToEntity(skuStr, ElectricSkuUpdateDTO.class);
        skuInfoService.updateSkuInfo(skuInfo);

        return JsonResult.doSuccessful();
    }

    @Override
    @PostMapping("/goods/sku/create/type")
    public JsonResult createGoodsType() {
        return null;
    }

    @Override
    @PostMapping("/goods/sku/change/status")
    public JsonResult changeSkuStatus(@RequestBody ElectricStatusUpdateDTO updateDTO) {
        if (updateDTO == null || updateDTO.getBaseId() == null || updateDTO.getStatus() == null) {
            throw new ServiceException(OperationResultCode.PARAMETER_ERROR);
        }
        boolean flag = skuInfoService.changeSkuStatus(updateDTO.getBaseId(), updateDTO.getStatus() );
        if (flag) {
            return JsonResult.updateSuccessFul();
        }
        return JsonResult.doFailed();
    }

    @Override
    @PostMapping("/sku/list")
    public JsonResult getSkuList(@RequestBody BackElectricSkuQueryDTO queryDTO) {
        if (queryDTO == null) {
            throw new ServiceException(OperationResultCode.PARAMETER_ERROR);
        }
        return JsonResult.doSuccessful(skuInfoService.getSkuList(queryDTO));
    }

    @Override
    @GetMapping("/sku/byid")
    public JsonResult getSkuListByGoodsId(@RequestParam("goodsId") Long goodsId) {
        return JsonResult.doSuccessful(skuInfoService.getSkuListByGoodsId(goodsId));
    }
}
